package com.workplan.client.widgets.historyitem;

import java.util.Date;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.general.client.action.ActionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;
import com.workplan.shared.datamodel.employeeshift.EmployeeShiftDataModel;

public class HistoryItemPresenterImpl extends
		WidgetPresenter<HistoryItemPresenter.Display> implements
		HistoryItemPresenter {

	private EmployeeShiftDataModel dataModel;

	public HistoryItemPresenterImpl(Display display, EventBus eventBus) {
		super(display, eventBus);
	}

	private void updateViewFromModel(EmployeeShiftDataModel dataModel) {
		display.getDate().setText(getFormattedDate(dataModel.getStartDate()));
		updateDisplayTotalTime(dataModel);
		display.getStartTime().setText(
				getFormattedHours(dataModel.getStartTime()));
		display.getEndTime().setText(getFormattedHours(dataModel.getEndTime()));
	}

	private void updateDisplayTotalTime(EmployeeShiftDataModel dataModel) {
		Date timesDiff = new Date(dataModel.getEndTime().getTime()
				- dataModel.getStartTime().getTime());
		display.getTotalTime().setText(getFormattedHours(timesDiff));
	}

	@Override
	public void setModel(EmployeeShiftDataModel dataModel) {
		this.dataModel = dataModel;
		updateViewFromModel(dataModel);
	}

	protected void persistData() {
		// TODO call service to update DM in server
	}

	@Override
	protected void onBind() {
		registerHandler(display
				.addStartTimeChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						Date d = parseTime(event.getValue());
						dataModel.setStartTime(d);
						updateDisplayTotalTime(dataModel);
						persistData();
					}
				}));

		registerHandler(display
				.addEndTimeChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						Date d = parseTime(event.getValue());
						dataModel.setEndTime(d);
						updateDisplayTotalTime(dataModel);
						persistData();
					}
				}));

		// TODO add handler to open remove modal

	}

	@SuppressWarnings("deprecation")
	private Date parseTime(String value) {
		String[] vals = value.split(":");
		Date d = new Date();
		d.setHours(Integer.valueOf(vals[0]));
		d.setMinutes(Integer.valueOf(vals[1]));
		return d;
	}

	@Override
	protected void onUnbind() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onRevealDisplay() {
	}

	@Override
	public HandlerRegistration addRemoveActionHandler(ActionHandler handler) {
		return display.addRemoveActionHandler(handler);
	}

	private String formatDate(Date date, String format) {
		// In case the time is 0 we don't want to show 1.1.1970
		if (date.getTime() == 0)
			return "";
		DateTimeFormat formater = DateTimeFormat.getFormat(format);
		return formater.format(date, TimeZone.createTimeZone(0));
	}

	private String getFormattedDate(Date date) {
		// In case the time is 0 we don't want to show 1.1.1970
		if (date.getTime() == 0)
			return "";
		return formatDate(date, getDateFormat());
	}

	private String getFormattedHours(Date date) {
		// In case the time is 0 we don't want to show 1.1.1970
		if (date.getTime() == 0)
			return "";
		return formatDate(date, getHoursFormat());
	}

	private String getDateFormat() {
		return "MM\\dd\\yyyy";
	}

	private String getHoursFormat() {
		return "HH:mm";
	}

}
